﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Management;
using System.Runtime;
using System.IO;
using System.Resources;

namespace Typheus
{
    public class ReadXML
    {
        public static string BookmarkPath =
               Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Typheus\\Bookmark.xml";
        public static string SettingsPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Typheus\\Settings.xml";
        public ReadXML()
        {
            
        }

        private static string LoadXmlFile(string File)
        {
            XmlDocument xmlDoc = new XmlDocument();
            StreamReader sr = new StreamReader(new FileStream(File, FileMode.Open, FileAccess.Read));
            string text= sr.ReadToEnd();
            sr.Close();
            return text;
        }

        public static string GetHomepage()
        {
            string ReturnValue = "";
            XmlDocument xmlDoc=new XmlDocument();
            xmlDoc.LoadXml(LoadXmlFile(SettingsPath));

            foreach (XmlElement element in xmlDoc.DocumentElement)
            {
                if (element.Attributes[0].Value == "HomePage")
                {
                    ReturnValue = element.Attributes[1].Value;
                }
            }
            return ReturnValue;
        }

        public static void SetHomePage(string HomePage)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(LoadXmlFile(SettingsPath));

            foreach (XmlElement element in xmlDoc.DocumentElement)
            {
                if (element.Attributes[0].Value == "HomePage")
                {
                    element.Attributes[1].Value=HomePage;
                }
            }
            StreamWriter sw = new StreamWriter(new FileStream(SettingsPath, FileMode.Truncate, FileAccess.Write));
            sw.Write(xmlDoc.OuterXml);
            sw.Close();
        }

        public static void AddBookMark(string BookMarkTitle, string BookMarkUrl)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(LoadXmlFile(BookmarkPath));
            
            xmlDoc.DocumentElement.InnerXml+="<bookmark id=\""+BookMarkTitle+"\" Url=\""+BookMarkUrl.Replace('=',';')+ "\"></bookmark>";
            StreamWriter sw = new StreamWriter(new FileStream(BookmarkPath, FileMode.Truncate, FileAccess.Write));
            sw.Write(xmlDoc.OuterXml);
            sw.Close();
        }

        public static Dictionary<string,string> GetBookMarkList()
        {
            Dictionary<string,string> results=new Dictionary<string, string>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(LoadXmlFile(BookmarkPath));
            foreach (XmlElement element in xmlDoc.DocumentElement)
            {
                results.Add(element.Attributes["id"].Value,element.Attributes["Url"].Value.Replace(';','='));
            }
           

            return results;
        }

        public static void RemoveBookMark(string value)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(LoadXmlFile(BookmarkPath));
            foreach (XmlNode node in xmlDoc.ChildNodes[0])
            {
                if (node.Attributes["id"].Value == value)
                {
                    xmlDoc.ChildNodes[0].RemoveChild(node);
                }
            }
        }

        public static void SetDebugMode(bool debug)
        {
            XmlDocument xmlDoc=new XmlDocument();
            xmlDoc.LoadXml(LoadXmlFile(SettingsPath));

            foreach (XmlElement element in xmlDoc.DocumentElement)
            {
                if (element.Attributes["id"].Value == "ScriptError")
                {
                    element.Attributes["value"].Value = debug.ToString();
                }
            }
            StreamWriter sw = new StreamWriter(new FileStream(SettingsPath, FileMode.Truncate, FileAccess.Write));
            sw.Write(xmlDoc.OuterXml);
            sw.Close();
        }

        public static bool GetDebugMode()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(LoadXmlFile(SettingsPath));
            bool debug=false;
            foreach (XmlElement element in xmlDoc.DocumentElement)
            {
                if (element.Attributes["id"].Value == "ScriptError")
                {
                    debug=Convert.ToBoolean(element.Attributes["value"].Value);
                }
            }
            return debug;
        }


        public static string GetDownloadDir()
        {
            string ReturnValue = "";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(LoadXmlFile(SettingsPath));

            foreach (XmlElement element in xmlDoc.DocumentElement)
            {
                if (element.Attributes[0].Value == "DownloadLocation")
                {
                    ReturnValue = element.Attributes[1].Value;
                }
            }
            return ReturnValue;
        }

        public static void SetDownloadDir(string DownloadDir)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(LoadXmlFile(SettingsPath));

            foreach (XmlElement element in xmlDoc.DocumentElement)
            {
                if (element.Attributes[0].Value == "DownloadLocation")
                {
                    element.Attributes[1].Value = DownloadDir;
                }
            }
            StreamWriter sw = new StreamWriter(new FileStream(SettingsPath, FileMode.Truncate, FileAccess.Write));
            sw.Write(xmlDoc.OuterXml);
            sw.Close();
        }
    }
}
